package ppOO;

import java.util.ArrayList;

public class NeuronLayer extends General {

   private ArrayList<ContPerceptron> neurons;
   private boolean isOutputLayer = false;

   public NeuronLayer(int numberOfNeurons,
         double[][] inputWeights, int layerIndex) {
      // Hidden Layer Constructor
      neurons = new ArrayList<ContPerceptron>();
     
      for (int i = 0; i < numberOfNeurons; i++) {
         NLPrintln("\tAdding New Neuron to Layer ("
               + (neurons.size()) + ") - input weights :: "
               + printArray(inputWeights[i]));
         neurons.add(new ContPerceptron(inputWeights[i],
               false, neurons.size(), layerIndex));
      }
      // Add bias 'fake'(fixed output) neuron.
      NLPrintln("\tAdding New (Bias)Neuron to Layer ("
            + (neurons.size()) + ")");
      neurons.add(new ContPerceptron(
            inputWeights[inputWeights.length - 1], true,
            neurons.size(), layerIndex));
   }

   public void setInput(double[] inputArray,
         String debugLayerIndex) {

      for (ContPerceptron neuron : neurons) {
         neuron.setInput(inputArray);
      }
   }

   public void setInput(double[] inputArray) {
      setInput(inputArray, "");
   }

   public double[] getLayerInput() {
      // it shouldn't matter which neuron we get the input since it should be
      // the same for each.
      // so get the input from the first neuron in the layer.

      return neurons.get(0).getInput();
   }

   public double[] getLayerOutput() {
      double[] output = new double[this.neurons.size()];

      for (int i = 0; i < this.neurons.size(); i++) {
         output[i] = this.neurons.get(i).calcOutput();
         if (DETAILED_DEBUG)
            NLPrintln("getLayerOutput :: "
                  + output[i]);
      }

      return output;
   }

   public ArrayList<ContPerceptron> getLayerNeurons() {
      return neurons;
   }

   public boolean isOutputLayer() {
      return isOutputLayer;
   }

   public void setOutputLayer(boolean isOutputLayer) {
      this.isOutputLayer = isOutputLayer;
   }

   public void remLastNeuron() {
      this.neurons.remove(this.neurons.size() - 1);

   }

   /* --------System Debug Code----------------------- */
   private void NLPrintln(String output) {
      output += "\n";
      NLPrint(output);
   }

   private void NLPrint(String output) {
      if (this.DEBUG)
         System.out.print("DEBUG Neural Layer:\t"
               + output);
   }

}
